<template>
    <div>
        <div class="head">
            首页 &nbsp;/ &nbsp;用户管理 &nbsp;/
            <span>添加</span>
        </div>
        <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
        >
            <el-form-item label="套餐名字" prop="account">
                <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="价格" prop="normalPrice">
                <el-input v-model="ruleForm.normalPrice"></el-input>
            </el-form-item>
            <el-form-item label="打折价格" prop="salePrice">
                <el-input v-model="ruleForm.salePrice"></el-input>
            </el-form-item>
            <el-form-item label="月份" prop="mouth">
                <el-input v-model="ruleForm.mouth"></el-input>
            </el-form-item>
            <el-form-item label="是否推荐" prop="type">
                <el-input v-model="ruleForm.type"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const {   mapActions } = createNamespacedHelpers("recipeModules");
export default {
  data(){
        return {
            ruleForm: {
                name: '',
                normalPrice: '',
                salePrice: '',
                mouth: '',
                type:''
              

            },
            rules: {
                name: [
                    { required: true, message: '请输入套餐名字', trigger: 'blur' },
                    { min: 2, max: 10, message: '长度在 2到 10个字', trigger: 'blur' }
                ],
                normalPrice: [
                    { required: true, message: '请输入价格', trigger: 'blur' },
                    { min: 1, max: 10, message: '', trigger: 'blur' }
                ],
                salePrice: [
                    { required: true, message: '请输入打折后的价格', trigger: 'blur' },
                    { min: 1, max: 10, message: '', trigger: 'blur' }
                ],
                mouth: [
                    { required: true, message: '请输入套餐月份', trigger: 'blur' },
                    { min: 1, max: 2, message: '', trigger: 'blur' }
                ],
                type: [
                    { required: true, message: '请输入套餐是否推荐', trigger: 'blur' },
                    { min: 1, max: 1, message: '1或者0', trigger: 'blur' }
                ],
            }
        }
  },
  methods:{
    ...mapActions(['asyncAddRecipe']),
         submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.asyncAddRecipe({
                        name:this.ruleForm.name,
                        normalPrice:this.ruleForm.normalPrice,
                        salePrice:this.ruleForm.salePrice,
                        mouth:this.ruleForm.mouth,
                        type:this.ruleForm.type,
                    })
                    alert('添加成功');

                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
  }

}
</script>

<style lang="scss" scoped>
.head {
    width: 1250px;
    height: 80px;
    border-bottom: 1px solid #ccc;
    text-align: left;
    line-height: 70px;
    font-size: 20px;
    color: #a4a4a4;
    span {
        color: black;
        font-weight: 600;
    }
}
</style>